/*
 *  Copyright (c) 2013 Hai Bison
 *
 *  See the file LICENSE at the root directory of this project for copying
 *  permission.
 */

package com.haibison.android.hutieu;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.annotation.TargetApi;
import android.os.AsyncTask;
import android.os.Build;
import android.os.ParcelFileDescriptor;
import android.util.Log;

import com.haibison.android.hutieu.utils.AsyncTaskCompat_v11;
import com.haibison.android.hutieu.utils.Settings;

/**
 * Helper class for {@link ShareProvider} in API 9+.
 * 
 * @author Hai Bison
 * 
 */
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class ShareProviderCompat_v9 {

    private static final String CLASSNAME = ShareProviderCompat_v9.class
            .getName();

    /**
     * This is singleton class.
     */
    private ShareProviderCompat_v9() {
    }// ShareProviderCompat_v9()

    /**
     * Creates new thread to read {@code inputStream} and write data to the
     * partner writer of returning file descriptor (which is a reader).
     * 
     * @param inputStream
     *            the input stream.
     * @return the reader.
     * @throws IOException
     *             if any.
     */
    public static ParcelFileDescriptor createStreamTransfer(
            final InputStream inputStream) throws IOException {
        if (BuildConfig.DEBUG)
            Log.d(CLASSNAME, "createReader()");

        ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe();

        final OutputStream outputStream = new ParcelFileDescriptor.AutoCloseOutputStream(
                pipe[1]) {

            @Override
            public void close() throws IOException {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    Log.e(CLASSNAME, e.getMessage(), e);
                }

                super.close();
            }// close()

        };

        AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                try {
                    int read = 0;
                    byte[] buf = new byte[Settings.FILE_BUFFER];
                    while ((read = inputStream.read(buf)) > 0 && !isCancelled())
                        outputStream.write(buf, 0, read);
                    outputStream.flush();

                    if (BuildConfig.DEBUG)
                        Log.d(CLASSNAME, "Transfering done");
                } catch (IOException e) {
                    Log.e(CLASSNAME, e.getMessage(), e);
                } finally {
                    try {
                        outputStream.close();
                    } catch (IOException e) {
                        Log.e(CLASSNAME, e.getMessage(), e);
                    }
                }

                return null;
            }// doInBackground()

        };

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
            task.execute();
        else
            AsyncTaskCompat_v11.executeOnExecutor(task,
                    AsyncTaskCompat_v11.Executor.THREAD_POOL);

        return pipe[0];
    }// createStreamTransfer()

}
